{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "tf.summary",
   "id": "2a68450a54e59ca3"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "1.tf.summary 模块在 TensorFlow 中用于记录训练过程中的关键信息，这些信息可以被 TensorBoard 可视化，帮助开发者监控和分析模型的训练过程。",
   "id": "f12712bc568e4960"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "2.tf.summary的主要用途：                                                                             （1）监控训练和验证过程中的损失和准确率等指标。                                                         （2）可视化模型的权重、偏差等参数的分布（直方图）。                                                      （3）展示模型输入和输出的图像或音频数据。                                                               （4）提供文本数据的记录，例如模型预测的标签或分类结果。\n",
   "id": "9432e10e45877414"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "常用的方法",
   "id": "70ed1c0d650809a8"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import os\n",
    "# 确保日志目录存在\n",
    "log_dir = 'logs'\n",
    "if not os.path.exists(log_dir):\n",
    "    os.makedirs(log_dir)\n",
    "# 创建一个摘要写入器\n",
    "writer = tf.summary.create_file_writer(log_dir)\n",
    "# 模拟一些数据\n",
    "step = 0\n",
    "loss_value = 0.25\n",
    "images = np.random.rand(2, 28, 28, 3)  # 随机生成一些图像数据\n",
    "weights = np.random.rand(10)  # 随机生成一些权重数据\n",
    "predictions = \"Sample prediction\"  # 模拟文本数据\n",
    "audio = np.sin(np.linspace(0, 20 * np.pi, 44100))  # 生成正弦波音频数据\n",
    "\n",
    "@tf.function\n",
    "def record_summaries(step, loss_value, images, weights, predictions, audio):\n",
    "    with writer.as_default():\n",
    "        # 记录标量摘要\n",
    "        tf.summary.scalar('loss', loss_value, step=step)\n",
    "        # 记录图像摘要\n",
    "        tf.summary.image('images', images, step=step)\n",
    "        # 记录直方图摘要\n",
    "        tf.summary.histogram('weights', weights, step=step)\n",
    "        # 记录文本摘要\n",
    "        tf.summary.text('predictions', predictions, step=step)\n",
    "        # 记录音频摘要\n",
    "        tf.summary.audio('audio', audio, sample_rate=44100, step=step)\n",
    "        writer.flush()  # 确保摘要被写入\n",
    "\n",
    "# 调用函数记录摘要\n",
    "record_summaries(step, loss_value, images, weights, predictions, audio)\n",
    "\n",
    "# 关闭摘要写入器\n",
    "writer.close()"
   ],
   "id": "e045033f5541233",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "tf.summary.scalar：记录标量值，如损失或准确率。",
   "id": "f51d74dfdd44c865"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import tensorflow as tf\n",
    "writer = tf.summary.create_file_writer('logs/scalars')\n",
    "with writer.as_default():\n",
    "  tf.summary.scalar('loss', 0.25, step=0)"
   ],
   "id": "8be3a77da1fca164",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "tf.summary.image：记录图像数据，如模型输入或生成的图像。",
   "id": "470c15bcc256ccbf"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "with writer.as_default():\n",
    "  tf.summary.image('input_images', images, step=0)"
   ],
   "id": "f87f938300c753d4",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "tf.summary.histogram：记录张量的分布，如模型权重的直方图。",
   "id": "b1720cdeb528e91d"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "with writer.as_default():\n",
    "  tf.summary.histogram('weights', weights, step=0)"
   ],
   "id": "f757123ca87d90d6",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "tf.summary.text：记录文本数据，如模型的预测结果。",
   "id": "f0723e56b0aee37d"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "with writer.as_default():\n",
    "  tf.summary.text('predictions', predictions, step=0)"
   ],
   "id": "217feefe64e59c4f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "tf.summary.audio：记录音频数据，如模型输入的语音信号。",
   "id": "7d04eb8438bb8aad"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "with writer.as_default():\n",
    "  tf.summary.audio('audio_signal', audio, sample_rate=44100, step=0)"
   ],
   "id": "4b934abb41bcd894",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
